import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/provider_test2/pages/provider_test_page2.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/widget/tab_view_widget1.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/widget/tab_view_widget2.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/widget/tab_view_widget3.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/widget/tab_view_widget4.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/widget/tab_view_widget5.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/widget/tab_view_widget6.dart';
import 'package:provider/provider.dart';

import 'provider_data/common_data.dart';

class ProviderTestPage extends StatefulWidget {
  @override
  _ProviderTestPageState createState() => _ProviderTestPageState();
}

class _ProviderTestPageState extends State<ProviderTestPage> with SingleTickerProviderStateMixin {
  TabController _tabController;
  CommonData _commonData = CommonData();
  final int _length = 6;

  @override
  void initState() {
    _tabController = TabController(length: _length, vsync: this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: Container(
          color: Colors.red,
          height: 100.h,
          child: FlatButton(
            onPressed: () {
              _commonData.changeUserModel();
            },
            child: Text("change model"),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
        appBar: AppBar(
          actions: <Widget>[
            InkWell(
              child: Icon(Icons.add),
              onTap: _providerTestPage2,
            )
          ],
          title: Text("provider test"),
          bottom: TabBar(
            controller: _tabController,
            tabs: _getTabs(),
            physics: BouncingScrollPhysics(),
          ),
        ),
        body: MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) => _commonData),
          ],
          child: TabBarView(
            controller: _tabController,
            children: <Widget>[
              TabViewWidget1(),
              TabViewWidget2(),
              TabViewWidget3(),
              Consumer<CommonData>(builder: (_, commonData, __) {
                return TabViewWidget4(
                  userModel: commonData.userModel,
                );
              }),
              Selector<CommonData, UserModel>(
                selector: (_, commonData) {
                  return commonData.userModel;
                },
                builder: (_, userModel, __) {
                  return TabViewWidget5();
                },
              ),
              Selector<CommonData, UserModel>(
                selector: (_, commonData) {
                  return commonData.userModel;
                },
                builder: (_, userModel, __) {
                  return TabViewWidget6(
                    userModel: userModel,
                  );
                },
              ),
            ],
          ),
        ));
  }

  List<Widget> _getTabs() {
    List<Widget> tabs = List();
    String tabTitle = "";
    for (int i = 0; i < _length; i++) {
      tabTitle = (i + 1).toString();
      if (4 == i) {
        tabTitle = "只改name";
      }
      tabs.add(Column(
        children: [
          Icon(
            Icons.ac_unit,
          ),
          Text(tabTitle)
        ],
      ));
    }
    return tabs;
  }

  void _providerTestPage2() {
    Navigator.push(context, CupertinoPageRoute(builder: (context) => ProviderTestPage2()));
  }
}
